﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NamesFight
{
    public partial class form : Form
    {
        
        public Random rand = new Random();

        public string[] fights = new string[] 
        {
            "攻击","冲撞","喵喵拳","元气弹",
            "咸鱼冲刺","口吐芬芳","居合斩","头槌",
            "摸头杀","重拳出击","飞刀","如来神掌",
            "毁天灭地","机枪扫射","扔石头","卖萌",
            "火球术","十字斩","无影脚","顺劈斩",
            "雷电术","偷袭"
        };

        public string[] atk = new string[]
        {
            "柔弱的","普通的","普通的","强力的","威力惊人的"
        };

        public int[] damages = new int[]
        {
            5,10,1,15,
            18,2,24,17,
            3,20,8,30,
            40,25,2,4,
            7,6,9,11,
            12,13,



        };

        public float[] extras = new float[]
        {
            0.5f,1.0f,1.0f,1.5f,2f
        };
        public form()
        {
            InitializeComponent();
            //初始化
            Control.CheckForIllegalCrossThreadCalls = false;
            //int ram = rand.Next(0, fights.Length);
            //fightList.Items.Add(nameA + fights[ram] + $"造成了{damages[ram]}点伤害");
            hpA.Value = hpA.Maximum;
            hpB.Value = hpB.Maximum;
        }


        //private delegate void SetBar(ProgressBar bar, int s);


        //private void SetBarVal(ProgressBar bar, int s)
        //{
        //    if (this.InvokeRequired)
        //    {
        //        SetBar _setBar = new SetBar(SetBarVal);
        //        this.Invoke(_setBar, new object[] { s });
        //    }
        //    else
        //    {
        //        bar.Value = Convert.ToInt32(s);
        //    }
        //}
        private void Fight(string A, string B)
        {
            LockEdit();
            //清空战斗记录
            if (fightList.Items.Count != 0)
            {
                fightList.Items.Clear();
            }
            //初始化血条
            hpA.Value = hpA.Maximum;
            hpB.Value = hpB.Maximum;
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(800);
                string win = FightProcess(A, B);
                fightList.Items.Add($"{win}取得了胜利！");
                AutoScr(fightList);
                ActEdit();
            });
            

        }

        private string FightProcess(string A, string B)
        {
            string fight;
            int ramAtk = 1;
            int ram,damage;
            int act = 0; //0-1攻击，2其他
            //A的回合

            act = rand.Next(0, 3); 
            if (act != 2) //攻击
            {
                ram = rand.Next(0, fights.Length);
                ramAtk = rand.Next(0, atk.Length);
                fight = fights[ram];
                damage = damages[ram];
                act = rand.Next(0, 11);
                if (act == 5 || act == 0)
                {
                    fightList.Items.Add(A + "使用了" + atk[ramAtk] + "『" + fight + "』" + $"，但是被{B}闪躲了！");
                    AutoScr(fightList);
                }
                else
                {
                    fightList.Items.Add(A + "使用了" + atk[ramAtk] + "『" + fight + "』" + $"造成了{damage * extras[ramAtk]}点伤害！");
                    AutoScr(fightList);
                    Damage(hpB, damage * extras[ramAtk]);
                }          
            }
            else //做其他事
            {
                act = rand.Next(0, 19);
                DoOtherThings(A, B, hpA, hpB, act);
            }
            if (hpA.Value <= 0 || hpB.Value <= 0)
                return hpA.Value <= 0 ? B : A;
            Thread.Sleep(1000);
            //B的回合

            act = rand.Next(0, 3);
            if (act != 2) //攻击
            {
                ram = rand.Next(0, fights.Length);
                ramAtk = rand.Next(0, atk.Length);
                fight = fights[ram];
                damage = damages[ram];
                act = rand.Next(0, 11);
                if (act == 5 || act == 0)
                {
                    fightList.Items.Add(B + "使用了" + atk[ramAtk] + "『" + fight + "』" + $"，但是被{A}闪躲了！");
                    AutoScr(fightList);
                }
                else
                {
                    fightList.Items.Add(B + "使用了" + atk[ramAtk] + "『" + fight + "』" + $"造成了{damage * extras[ramAtk]}点伤害！");
                    AutoScr(fightList);
                    Damage(hpA, damage * extras[ramAtk]);
                }
            }
            else
            {
                act = rand.Next(0, 19);
                DoOtherThings(B, A, hpB, hpA, act);
            }
            if (hpA.Value <= 0 || hpB.Value <= 0)
                return hpA.Value <= 0 ? B : A;
            Thread.Sleep(1000);
            return FightProcess(A, B);
        }

        private void DoOtherThings(string me, string you, ProgressBar mine,
            ProgressBar yours, int id)
        {
            switch (id)
            {
                case 0: //滑倒
                    {
                        fightList.Items.Add($"{me}滑倒了！受到了5点伤害！");
                        Damage(mine, 5);
                        break;
                    }
                case 1: //喝药
                    {
                        fightList.Items.Add($"{me}喝了一杯奶茶，恢复了5点生命！");
                        Damage(mine, -5);
                        break;
                    }
                case 2: //有毒
                    {
                        fightList.Items.Add($"{me}喝了一杯奶茶，由于太难喝损失了5点生命！");
                        Damage(mine, 5);
                        break;
                    }
                case 3: //挑衅
                    {
                        fightList.Items.Add($"{me}对{you}使用了挑衅！");
                        break;
                    }
                case 4: //可乐
                    {
                        fightList.Items.Add($"{me}喝了一杯肥宅快乐水，恢复了10点生命！");
                        Damage(mine, -10);
                        break;
                    }
                case 5: //天谴
                    {
                        fightList.Items.Add($"{me}遭到了天谴，生命值减半！");
                        Damage(mine, mine.Value * 0.5f);
                        break;
                    }
                case 6: //咸鱼
                    {
                        fightList.Items.Add($"{me}突然失去了梦想，损失了25点生命！");
                        Damage(mine, 25);
                        break;
                    }
                case 7: //思索
                    {
                        fightList.Items.Add($"{me}思考了生命的意义，恢复了1点生命！");
                        Damage(mine, -1);
                        break;
                    }
                case 8: //秘药
                    {
                        fightList.Items.Add($"{me}使用了『秘制HP药』，恢复了30点生命！");
                        Damage(mine, -40);
                        break;
                    }
                case 9: //秘药失败
                    {
                        fightList.Items.Add($"{me}使用了『秘制HP药』，中毒损失了20点生命！");
                        Damage(mine, 20);
                        break;
                    }
                case 10: //神助
                    {
                        fightList.Items.Add($"{me}受到了神助，恢复了50点生命！");
                        Damage(mine, -50);
                        break;
                    }
                case 11: //咬
                    {
                        fightList.Items.Add($"{me}咬了{you}一口，未能造成伤害！");
                        break;
                    }
                case 12: //强咬
                    {
                        fightList.Items.Add($"{me}咬了{you}一口，造成1点真实伤害！");
                        Damage(yours, 1);
                        break;
                    }
                case 13: //嘴遁
                    {
                        fightList.Items.Add($"{me}劝说{you}以礼来降，造成5点精神伤害！");
                        Damage(yours, 5);
                        break;
                    }
                case 14: //失败
                    {
                        fightList.Items.Add($"{me}试图劝说{you}以礼来降，但{you}不为所动！");
                        break;
                    }
                case 15: //交兵
                    {
                        fightList.Items.Add("双方近身交手，各受到5点伤害！");
                        Damage(mine, 5);
                        Damage(yours, 5);
                        break;
                    }
                case 16: //休息
                    {
                        fightList.Items.Add($"{me}原地休息，恢复了15点生命！");
                        Damage(mine, -15);
                        break;
                    }
                case 17: //愣住
                    {
                        fightList.Items.Add($"{me}愣住了！");
                        break;
                    }
                case 18: //怀疑人生
                    {
                        fightList.Items.Add($"{me}开始怀疑人生。");
                        break;
                    }
                default:
                    break;
            }
            AutoScr(fightList);
        }

        private void Damage(ProgressBar bar, float damage)
        {
            if (damage >= 0) //受伤
            {
                if (bar.Value <= damage)
                    bar.Value = 0;
                else
                    bar.Value -= (int)damage;
            }
            else //加血
            {
                if (bar.Value - damage >= bar.Maximum)
                    bar.Value = bar.Maximum;
                else
                    bar.Value -= (int)damage;
            }
        }

        /// <summary>
        /// ListBox自动滚动到底部
        /// </summary>
        /// <param name="listBox"></param>
        private void AutoScr(ListBox listBox)
        {
            if (listBox.Items.Count >= 20)
                listBox.TopIndex = listBox.Items.Count - (int)(listBox.Height / listBox.ItemHeight);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string A = nameA.Text;
            string B = nameB.Text;
            if (A != "" && B != "")
            {
                Fight(A, B);
            }
            else
                MessageBox.Show("姓名不可为空！");
        }

        /// <summary>
        /// 锁定姓名编辑及开始按钮
        /// </summary>
        private void LockEdit()
        {
            startBtn.Enabled = false;
            nameA.Enabled = false;
            nameB.Enabled = false;
        }
        /// <summary>
        /// 激活全部编辑区
        /// </summary>
        private void ActEdit()
        {
            startBtn.Enabled = true;
            nameA.Enabled = true;
            nameB.Enabled = true;
        }
    }
}
